/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : fTimeGenerator.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 13, 2009       Raghu Sarraff			       Created
 */
package net.lwf.handler.time.util;

import net.lwf.number.util.RandomNumberGenerator;

public class TimeGenerator {
	
	private static int[] multiples = {2,3,4,5,6,10,12,15,20,30};
	
	/**
	 * Generates only hours.
	 * 
	 * @return Time
	 */
	public static Time generateH() {
		Time time = generateTime(true, false, false);
		return time;
	}

	/**
	 * Generates only hours and minutes.
	 * 
	 * @return Time
	 */
	public static Time generateHM() {
		Time time = generateTime(true, true, false);
		return time;
	}
	
	/**
	 * Generates only hours, minutes and seconds.
	 * 
	 * @return Time
	 */
	public static Time generateHMS() {
		Time time = generateTime(true, true, true);
		return time;
	}
	
	/**
	 * Generates hours and multiples minutes and multiple seconds. Generated minutes and seconds 
	 * are multiples of 2 or 3 or 4 or 5 or 6 or 10 or 12 or 15 or 20 or 30.
	 * 
	 * @return Time
	 */
	public static Time generateHMMMS() {
		int hours = RandomNumberGenerator.generate(1, 24);
		//for minutes
		int multiple = multiples[RandomNumberGenerator.generate(0, multiples.length-1)];
		int factor = 60 / multiple;
		int minutes = multiple*RandomNumberGenerator.generate(0, factor-1);
		//for seconds
		multiple = multiples[RandomNumberGenerator.generate(0, multiples.length-1)];
		factor = 60 / multiple;
		int seconds = multiple*RandomNumberGenerator.generate(0, factor-1);
		
		return new Time(hours, minutes, seconds);
	}
	
	/**
	 * Generates hours and multiples minutes. Generated minutes  
	 * are multiples of 2 or 3 or 4 or 5 or 6 or 10 or 12 or 15 or 20 or 30.
	 * The seconds are zeros.
	 * 
	 * @return Time
	 */
	public static Time generateHMM() {
		int hours = RandomNumberGenerator.generate(1, 24);
		int multiple = multiples[RandomNumberGenerator.generate(0, multiples.length-1)];
		int factor = 60 / multiple;
		int minutes = multiple*RandomNumberGenerator.generate(1, factor-1);
		return new Time(hours, minutes, 0);
	}
	
	/**
	 * Generates hours and minutes. The minutes are multiples of the given input.
	 * The input param value must be
	 * 2 or 3 or 4 or 5 or 6 or 10 or 12 or 15 or 20 or 30.
	 * 
	 * @param minmultiple Minute multiple
	 * 
	 * @return
	 */
	public static Time generateHMM(int minmultiple) {
		int hours = RandomNumberGenerator.generate(1, 24);
		int factor = 60 / minmultiple;
		int minutes = minmultiple*RandomNumberGenerator.generate(1, factor-1);
		return new Time(hours, minutes, 0);
	}
	
	/**
	 * Generates hours and minutes. The minutes and seconds are multiples of the given input.
	 * The input param value must be
	 * 2 or 3 or 4 or 5 or 6 or 10 or 12 or 15 or 20 or 30.
	 * 
	 * @param minmultiple Minutes multiple
	 * @param secmultiple Seconds multiple 
	 * 
	 * @return
	 */
	public static Time generateHMMMS(int minmultiple, int secmultiple) {
		int hours = RandomNumberGenerator.generate(1, 24);
		//for minutes
		int factor = 60 / minmultiple;
		int minutes = minmultiple*RandomNumberGenerator.generate(1, factor-1);
		//for seconds
		factor = 60 / secmultiple;
		int seconds = secmultiple*RandomNumberGenerator.generate(1, factor-1);
		
		return new Time(hours, minutes, seconds);
	}
	
	/**
	 * Generates the time. Hours are generated on 24 hour format. 
	 * Minutes and seconds are generated multiples of 5.
	 * 
	 * @param usehours		
	 * @param useminutes
	 * @param useseconds
	 * 
	 * @return Time
	 */
	public static Time generateTime(boolean usehours, boolean useminutes, boolean useseconds) {
		int hours = 0;
		int minutes = 0;
		int seconds = 0;
		
		if(usehours) {
			hours = RandomNumberGenerator.generate(1, 24);
		}
		if(useminutes) {
			minutes = 5*RandomNumberGenerator.generate(0, 12);
		}
		if(useseconds) {
			seconds = 5*RandomNumberGenerator.generate(0, 12);
		}
		Time time = new Time(hours, minutes, seconds);
		return time;
	}
	
	/**
	 * Generates the time. Hours are generated on 12 hour format. 
	 * Minutes and seconds are generated multiples of 5.
	 * 
	 * @param usehours		
	 * @param useminutes
	 * @param useseconds
	 * @param is12hrtime
	 *  
	 * @return Time
	 */
	public static Time generateTime(boolean usehours, boolean useminutes, boolean useseconds, boolean is12hrtime) {
		int hours = 0;
		int minutes = 0;
		int seconds = 0;
		
		if(usehours) {
			hours = RandomNumberGenerator.generate(1, 24);
		}
		if(useminutes) {
			minutes = 5*RandomNumberGenerator.generate(0, 12);
		}
		if(useseconds) {
			seconds = RandomNumberGenerator.generate(0, 60);
		}
		Time time = new Time(hours, minutes, seconds);
		return time;
	}
	
	public static void main(String[] args) {
		/*
		Working....... Commented.....
		
		System.out.println("...... Generates only Hours.......");
		generateH().print();
		System.out.println(" ");
		
		System.out.println("...... Generates Hours and Mintes only .......");
		generateHM().print();
		System.out.println(" ");
		
		System.out.println("...... Generates Hours, minutes and seconds .......");
		generateHMS().print();
		System.out.println(" ");
		
		System.out.println("...... Generates Hours and minute multiples .......");
		generateHMM().print();
		System.out.println(" ");
		
		System.out.println("...... Generates Hours and minutes (multipe: 10) .......");
		generateHMM(10).print();
		System.out.println(" ");
		
		System.out.println("...... Generates Hours and (minute and seconds) multiples .......");
		generateHMMMS().print();
		System.out.println(" ");
		
		System.out.println("...... Generates Hours, minutes(multipe: 5) and seconds(multiple:15) .......");
		generateHMMMS(5, 15).print();
		*/
		Time time1 = generateTime(true, true, false);
		Time time2 = generateTime(true, true, false);
		long elapsed = TimeUtil.getElapsedTime(time1, time2);
/*		System.out.println("=======> Elapsed time: " + elapsed);
		if(elapsed < 0) {
			elapsed = -(elapsed);
			System.out.println("Time 2 occurs before Time1");
		}
		else {
			System.out.println("Time 2 occurs after Time1");
		}
		Time time = new Time(0, 0, elapsed);
		time.print();
*/
	}
}

